package api

import (
	"longmen/server/internal/rpc/sport/server"
	"longmen/server/internal/services/nami/services/sports/basketball/bbcache"
	"longmen/server/internal/services/nami/services/sports/football/fbcache"
	"longmen/server/pkg/common/api_models"
	_const "longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	protobuf2 "longmen/server/pkg/protobuf"

	"github.com/jinzhu/copier"
	"github.com/labstack/echo/v4"
)

// 帮忙去掉 grpc 返回的 多余 字段.
type LineupFootball struct {
	SportId uint32 `json:"sport_id"`
	MatchId uint32 `json:"match_id"`
	// 足球
	HomeFormationFootball string                            `json:"home_formation"`
	AwayFormationFootball string                            `json:"away_formation"`
	HomePlayersFootball   []*protobuf2.LineupPlayerFootball `json:"home_players"`
	AwayPlayersFootball   []*protobuf2.LineupPlayerFootball `json:"away_players"`
}
type LineupBasketball struct {
	SportId uint32 `json:"sport_id"`
	MatchId uint32 `json:"match_id"`
	// 篮球
	HomePlayersBasketball []*protobuf2.LineupPlayerBasketball `json:"home_players"`
	AwayPlayersBasketball []*protobuf2.LineupPlayerBasketball `json:"away_players"`
}

// @Summary 比赛战队信息
// @Description 比赛战队信息
// @Accept  json
// @Produce  json
// @Tags Lineup
// @Param   sendBody body protobuf.GetLineupByMatchReq true "body"
// @Success 200 {object} protobuf.GetLineupByMatchResp "result"
// @Router /api/lineup/getLineupByMatch [POST]
func (base *Controller) GetLineupByMatch(c echo.Context) error {
	// parse request,
	req := &protobuf2.GetLineupByMatchReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}

	// grpc context,
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()

	// grpc request
	resp, err := grpc_client.GrpcSportServer.GetLineupByMatch(ctx, req)
	if err != nil {
		return util.GrpcError(c, _const.RPC_FAILED, _const.EMPTY_STR, err)
	}

	// check rpc code,
	if resp.Code != server.CodeSuccess {
		return util.BuildFail(c, int(resp.Code), resp.Message, nil)
	}

	// check rpc data, to avoid panic,
	if resp.Lineup == nil {
		return util.BuildFail(c, 1, "没有数据", nil)
	}

	var lineupSpecific any
	switch resp.Lineup.SportId {
	case _const.FOOT_BALL_SPORT_ID:
		lineupSpecific = &LineupFootball{}
		err = copier.Copy(lineupSpecific, resp.Lineup)
		if err != nil {
			return util.BuildFail(c, 1, "Fail", err)
		}
	case _const.BASKET_BALL_SPORT_ID:
		lineupSpecific = &LineupBasketball{}
		err = copier.Copy(lineupSpecific, resp.Lineup)
		if err != nil {
			return util.BuildFail(c, 1, "Fail", err)
		}
	default:
		return util.BuildFail(c, 1, server.MsgMatchIdNotFound, nil)
	}

	return util.BuildSuccessWithStatus(c, lineupSpecific, resp.Message, int(resp.Code))
}

func (base *Controller) Lineup(c echo.Context) error {
	req := &api_models.LineupReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}

	if req.SportID == 1 {
		f := fbcache.NewLineUpRedis()
		ret := f.GetLineUpCache(req.MatchID)
		return util.BuildSuccess(c, ret, "")
	}

	if req.SportID == 2 {
		f := bbcache.NewLineUpCache()
		ret := f.GetLineUpCache(req.MatchID)
		return util.BuildSuccess(c, ret, "")
	}

	return util.BuildFail(c, 1, "", nil)
}

func (base *Controller) Score(c echo.Context) error {
	req := &api_models.LineupReq{}
	if err := util.Json2Struct(c.Get(_const.HTTP_KEY_BODY).(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}

	if req.SportID == 1 {
		f := fbcache.NewLineUpRedis()
		ret := f.GetScoreCache(req.MatchID)
		return util.BuildSuccess(c, ret, "")
	}

	if req.SportID == 2 {
		f := bbcache.NewLineUpCache()
		ret := f.GetLineUpCache(req.MatchID)
		return util.BuildSuccess(c, ret, "")
	}

	return util.BuildFail(c, 1, "", nil)
}
